package com.hnchances.grade.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.hnchances.grade.common.utils.PageUtils;
import com.hnchances.grade.common.utils.R;
import com.hnchances.grade.vo.GradesVo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hnchances.grade.entity.GradesEntity;
import com.hnchances.grade.service.GradesService;



/**
 * 
 *
 * @author lpj
 * @email ${email}
 * @date 2022-08-27 15:10:23
 */
@RestController
@RequestMapping("grade/grades")
public class GradesController {
    @Autowired
    private GradesService gradesService;

    /**
     * 分页查询
     * 只能查询本本班级学生的成绩
     * @param params 分页参数
     * @return
     */
    @ApiOperation(value = "查询本班级的学生的成绩",notes = "分页查询，可以根据所携带参数进行不同条件的查询")
    @RequiresRoles("老师")
    @PostMapping("/list")
    //@RequiresPermissions("grade:grades:list")
    public R list(@RequestParam Map<String, Object> params, @RequestHeader("Authorization") String token){
//        PageUtils page = gradesService.queryPageByCondition(params, token);
        R r = gradesService.queryPageByCondition(params, token);
        return r;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "查询单个学生的成绩")
    @RequiresRoles("老师")
    @GetMapping("/info/{id}")
    //@RequiresPermissions("grade:grades:info")
    public R info(@PathVariable("id") Long id){
		GradesEntity grades = gradesService.getById(id);
        return R.ok().put("grades", grades);
    }

    /**
     * 保存新增
     * @param gradesVos
     * @return
     */
    @RequiresRoles("老师")
    @PostMapping("/save")
    @ApiOperation(value = "老师录入本班学生的成绩")
    //@RequiresPermissions("grade:grades:save")
    public R save(@RequestBody List<GradesVo> gradesVos){
        R r = gradesService.saveDetails(gradesVos);
        return r;
    }

    /**
     * 修改成绩信息
     * @param grades
     * @return
     */
    @RequiresRoles("老师")
    @PostMapping("/update")
    @ApiOperation(value = "修改学生成绩")
    //@RequiresPermissions("grade:grades:update")
    public R update(@RequestBody GradesEntity grades){
		gradesService.updateById(grades);
        return R.ok();
    }

    /**
     * 根据id删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据id进行批量或者单独删除")
    @RequiresRoles("老师")
    @PostMapping("/delete")
    // @RequiresPermissions("grade:grades:delete")
    public R delete(@RequestBody Integer[] ids){
		gradesService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
